﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;

namespace FourKill.Engine
{
    public class WanderingPersonGameObject : BaseGameObject, IMovementObservable
    {
        private const int BOUNDRY_SIZE = 500;
        private Sprite _sprite;
        private RectangleF _boundries = RectangleF.Empty;
        private int _moveAwayCounter = 0;
        private int _turnStep = 5;
        private int _turnAmount = 0;
        private int _turnDirection = 1;
        public event EventHandler<MovementArgs> Moved;

        public WanderingPersonGameObject()
        {
            var r = _rand.Next(4);
            Speed = r > 2 ? 0 : r;
        }

        public override void Tick()
        {
            double distance = 0;
            if (_turnAmount > 0 )
            {
                int amt = _turnStep;
                if ((_turnAmount - _turnStep) > 0)
                    _turnAmount -= _turnStep;
                else
                {
                    amt = _turnAmount;
                    _turnAmount = 0;
                }

                this.Turn(Utility.Radians(amt * _turnDirection));
            }
            else
            {

                if (_boundries == RectangleF.Empty)
                {
                    _boundries = new RectangleF(Convert.ToSingle(this.X) - BOUNDRY_SIZE, Convert.ToSingle(this.Y) - BOUNDRY_SIZE, BOUNDRY_SIZE * 2, BOUNDRY_SIZE * 2);
                    _moveAwayCounter = 0;
                }
                else
                {
                    if (!_boundries.Contains(this.PointF))
                    {
                        if (_moveAwayCounter == 0)
                        {
                            _turnAmount = _rand.Next(150, 210);
                            _turnDirection = _turnAmount < 0 ? -1 : 1;
                            _turnAmount = Math.Abs(_turnAmount);
                            //this.Turn(Utility.Radians(_turnAmount));
                            _moveAwayCounter = _rand.Next(4, 12);
                        }
                    }
                    else
                    {
                        _moveAwayCounter = 0;
                    }
                }

                if (_moveAwayCounter == 0)
                {
                    if (Speed > 0)
                    {
                        var r = _rand.Next(100);
                        if (r > 98) Speed = 0;
                        else if (r < 10)
                        {
                            _turnAmount = _rand.Next(-60, 60);
                            _turnDirection = _turnAmount < 0 ? -1 : 1;
                            _turnAmount = Math.Abs(_turnAmount);
                            //this.Turn(Utility.Radians(_turnAmount));
                        }
                    }
                    else
                    {
                        var r = _rand.Next(50);
                        if (r == 25) Speed = 1 + _rand.NextDouble() * 2;
                        else if (r == 45)
                        {
                            _turnAmount = _rand.Next(-90, 90);
                            _turnDirection = _turnAmount < 0 ? -1 : 1;
                            _turnAmount = Math.Abs(_turnAmount);
                            //this.Turn(Utility.Radians(_rand.Next(-90, 90)));
                        }
                    }
                }
                else
                {
                    _moveAwayCounter--;
                }

                if (Speed > 0) this.Move(Speed);
                distance += Speed;
                
            }
            var placement = new Placement
                                {
                                    X=this.X,
                                    Y=this.Y,
                                    Z=this.Z,
                                    Bearing=this.Bearing, 
                                    TotalMovement = this.TotalMovement
                                };

            if (Moved != null)
            {
                Moved(this, new MovementArgs { Placement = placement }); 
            }
        }

        public Sprite Sprite
        {
            get 
            {
                _sprite = _sprite ?? Sprite.Instance(SpriteSheets.GetRandomCreatureSpriteSheet());
                return _sprite;
            }
        }

        public override IPaintable Painter
        {
            get
            {
                return Sprite;
            }
        }
    }
}
